package sk.tuke.android4all.messenger;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnGenericMotionListener;
import android.widget.TextView;
import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;

public class NewMessageFragment extends CustomFragment{

	private static String TAG = "NewMessageFragment";
	private Activity mActivity = null;
	private int selectedTag = 0;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	
	private boolean receiverSet = false;
	private boolean textSet = false;
	
	public NewMessageFragment(Activity aActivity_in){
		mActivity = aActivity_in;
		selectedTag = 0;
		mSpeechEngine = new Tts(mActivity);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_newmessage, container, false);	       
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        createUI();
    }
	
	private void createUI() {
		mActionListener = new SwipeListener(this.mActivity) {
	        @Override
	        public void onDoubleTapAction(int count) {
	            Log.i("TAG", "DoubleTapped " + selectedTag);
	            resetTouchCounter();
	            if(selectedTag != 0){
	            	FragmentTransaction f = mActivity.getFragmentManager().beginTransaction();
	            	if(selectedTag == R.id.receiver){
	            		receiverSet = true;
	            		setEditText(selectedTag);
	            	}else if(selectedTag == R.id.text){
	            		textSet = true;
	            		setEditText(selectedTag);
	            	}else if(selectedTag == R.id.send){
	            		String receiverText = ((TextView) getView().findViewById(R.id.receiver)).getText().toString();
	            		String messageText = ((TextView) getView().findViewById(R.id.text)).getText().toString();
	            		sendMessage(receiverText, messageText);
	            	}
	            }
	        }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				 
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		
		getView().setOnTouchListener(mActionListener);
		 
		final TextView receiverEdit = (TextView) getView().findViewById(R.id.receiver);
		final TextView textEdit = (TextView) getView().findViewById(R.id.text);
		final TextView sendBtn = (TextView) getView().findViewById(R.id.send);
				
		receiverEdit.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent event) {
				mActionListener.resetTouchCounter();
				String textInControl = receiverEdit.getText().toString();
                if (selectedTag != R.id.receiver && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    if(receiverSet){
                    	mSpeechEngine.sayMessage("Receiver: " + textInControl);
                    }else{
                    	mSpeechEngine.sayMessage("Receiver: tap to edit");
                    }
                    
                    selectedTag = R.id.receiver;
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		textEdit.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = textEdit.getText().toString();
                if (selectedTag != R.id.text && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    if(textSet){
                    	mSpeechEngine.sayMessage("Message: " + textInControl);
                    }else{
                    	mSpeechEngine.sayMessage("Message: tap to edit");
                    }
                    selectedTag = R.id.text;
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		sendBtn.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = sendBtn.getText().toString();
                if (selectedTag != R.id.send && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = R.id.send;
                }else{
                    touchCounter++;
                }
                return false;
			}});
	}
	
	@Override
	public void returnAction() {
		mActivity.finish();
	}
	
	@Override
	public void proceedReturnedResult(int requestCode, int resultCode, Intent in){
		Log.i(TAG, "proceedReturnedResult");
	 	((TextView)getView().findViewById(requestCode)).setText(in.getExtras().getString("text"));
    }
	
	private void sendMessage(String receiverText, String messageText) {
		SmsManager sms = SmsManager.getDefault();
	    sms.sendTextMessage(receiverText, null, messageText, null, null);
	}
	
	private void setEditText(int aControlId) {		
		Intent mTextRequest = new Intent("sk.tuke.android4all.textprovider");
		mTextRequest.putExtra("text", "");
		mTextRequest.putExtra("caller", getClass().getName());
		mActivity.startActivityForResult(mTextRequest, aControlId);
	}

}
